package zuo.algo.c05;

import zuo.algo.data.LinkedNode;
import zuo.algo.util.LinkedUtils;

/**
 * 01:10-翻转单链表
 */
public class ReverseList {

    public static LinkedNode reverse(LinkedNode root) {
        LinkedNode newRoot = root;
        while (root.next != null) {
            LinkedNode next = root.next;
            root.next = next.next;
            next.next = newRoot;
            newRoot = next;
        }
        return newRoot;
    }

    public static void main(String[] args) {
        LinkedNode root = LinkedUtils.constructLinkedList(new int[]{1, 2, 3, 4, 5, 6});
        LinkedUtils.printLinked(root);
        root = reverse(root);
        LinkedUtils.printLinked(root);
    }
}
